<?php
/**
 * Created by PhpStorm.
 * User: linyh
 * Date: 2015/10/5
 * Time: 14:34
 */

namespace app\lib;

class CommonHelp {
    public static function friendlyTime($time, $showLong=true) {
        if (!is_numeric($time)) {
            $time = strtotime($time);
        }
        $htime = date("H:i", $time);
        $time_diff = time() - $time;

        $minute = 60;
        $hour = $minute * 60;
        $day = $hour * 24;
        $week = $day * 7;
        $year = $day * 365;
        $dayDiff = (strtotime(date("Y-m-d")) - strtotime(date("Y-m-d", $time))) / $day;
        if ($time_diff < $minute) {
            $str = '刚刚';
        } elseif ($time_diff < $hour) {
            $min = floor($time_diff / 60);
            $str = "{$min}分钟前";
        } elseif ($time_diff < $day && $dayDiff == 0) {
            $h = floor($time_diff / (60 * 60));
            $str = "{$h}小时前";
        } elseif ($time_diff < $week) {
            if ($dayDiff == 1) {
                $str = "昨天 $htime";
            } elseif ($dayDiff == 2) {
                $str = "前天 $htime";
            } else {
                $str = "{$dayDiff}天前 $htime";
            }
        } else {
            if ($showLong) {
                if (date("Y", $time) == date("Y")) {
                    $str = date("m月d日", $time);
                } else {
                    $str = date("Y年m月d日", $time);
                }
            } else {
                $str = "一周前";
            }
        }
        return $str;
    }

    public static function fileType($filename){
        $suffix = substr($filename, strrpos($filename, '.') + 1);
        switch ($suffix) {
            case 'zip':
            case 'rar':
            case 'r00':case 'r01':case 'r02':case 'r03':case 'r04':
            case 'r05':case 'r06':case 'r07':case 'r08':case 'r09':
                $suffixType = "rar";
                break;
            case 'doc':
            case 'docx':
            case 'pages':
                $suffixType = "word";
                break;
            case 'ppt':
            case 'pptx':
            case 'key':
                $suffixType = "ppt";
                break;
            case 'csv':
            case 'xls':
            case 'xlsx':
            case 'numbers':
                $suffixType = "excel";
                break;
            case 'pdf':
                $suffixType = "pdf";
                break;
            case 'bmp': case 'jpg': case 'jpeg': case 'png': case 'tiff': case 'gif': case 'exif': case 'svg':
                $suffixType = "pic";
                break;
            case 'flac': case 'ape': case 'wav': case 'mp3': case 'aac': case 'ogg': case 'wma':
            case 'mp4': case 'mov': case 'm4a': case 'avi':
                $suffixType = "media";
                break;
            case 'psd':
            case 'rp': case 'rplib':
            case 'sketch':
                $suffixType = "rp";
                break;
            case 'md':
            case 'txt':
            case 'html':
            case 'htm':
            case 'chm':
            case 'js':
            case 'php':
            case 'aspx':
            case 'c':
            case '':
                $suffixType = "txt";
                break;
            default:
                $suffixType = "other";
        }
        return $suffixType;
    }

    public static function greeting() {
        $textIndex = "000111222334455556666667";
        $texts = ['夜深了，','凌晨了，','早上好！','上午好！','中午好！','下午好！','晚上好！','夜深了，'];
        return $texts[(int)$textIndex[(int)date('G')]];
    }

    public static function getQrcodeUrl($text) {
        $url = 'http://qr.liantu.com/api.php?fg=333333&el=l&w=150&m=0&text=' . urlencode($text);
        return $url;
    }

    public static function moneyCompare($money1, $money2) {
        return round((double)$money1 * 100) == round((double)$money2 * 100);
    }
}